<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\MemberService;
use App\Services\OrderService;
use Session;
use App\Services\CourseTypeService;
use App\Services\CourseService;
use App\Services\TagService;
use Illuminate\Http\Request;
use App\Services\OSS;
use App\Services\CourseContentService;


class CourseController extends Controller{
    public function GiveCourseList($courseid){
        $order = new OrderService;
        $member = new MemberService;

        $orderlist = $order->getGiveCourseList($courseid);

        if($orderlist !== false){
            foreach($orderlist as $otm=>$ost){
                $memberInfo = $member->getMemberInfoByid($ost['member']);
                // 生产环境中需要增加判断条件，即$memberInfo->id > 116
                if($memberInfo->nick == base64_encode(base64_decode($memberInfo->nick))){
                    $memberInfo->nick = base64_decode($memberInfo->nick);
                }

                $orderlist[$otm]['nick'] = $memberInfo->nick;
                $orderlist[$otm]['telephone'] = $memberInfo->telephone ? $memberInfo->telephone : '暂无联系电话';
            }

            $data = array('code'=>200,'list'=>$orderlist);
        }else{
            $data = array('code'=>400);
        }
        return response()->json($data);
    }

    public function giveCourse($id){
        $member = new MemberService;
        $order = new OrderService;

        // 获取会员列表
        $mc = $member->getMemberCount();
        $memberlist = $member->getMemberList($mc,'');
        $orderlist = $order->getGiveCourseList($id,array());
        $member_id_list = array();

        if($orderlist !== false){
            foreach($orderlist as $otm=>$ost){
                $member_id_list[] = $ost['member'];
            }
        }

        if($memberlist !== false){
            foreach($memberlist as $mtm=>$mst){
                if(in_array($mst->id,$member_id_list)){
                    $memberlist[$mtm]->is_checked = 1;
                }else{
                    $memberlist[$mtm]->is_checked = 0;
                }
            }
            $_data = array('code'=>200,'courseid'=>$id,'member_list'=>$memberlist);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }

    public function HandleGiveCourse(){
        $order = new OrderService;
        $course = new CourseService;

        $userid = addslashes($_POST['userid']);
        $courseid = intval($_POST['courseid']);

        if(strpos($userid,',') !== false){
            $userlist = explode(',',$userid);
        }else{
            $userlist = array($userid);
        }
        rsort($userlist);
        // var_dump($userlist);

        $courseinfo = $course->getCourseInfoById($courseid);
        $orderlist = $order->getGiveCourseList($courseid,$userlist);

        if(!empty($orderlist)){
            $order_member_id_list = array();
            foreach($orderlist as $otm=>$ost){
                if(in_array($ost->member_id,$userlist)){
                    // unset($userlist[$otm]);
                    $order_member_id_list[] = $ost->member_id;
                }
            }
        }else{
            $order_member_id_list = array();
        }
        $list = array_diff($userlist,$order_member_id_list);

        if(!empty($list)){
            foreach($list as $utm=>$ust){
                $order_data['member'] = intval($ust);
                $order_data['price'] = $courseinfo->price;

                $orderid = $order->insertOrderBySave($order_data);
                if($orderid){
                    $order_product_data['orderid'] = $orderid;
                    $order_product_data['courseid'] = $courseid;

                    $res1 = $order->insertOrderProductBySave($order_product_data);
                    // var_dump($res1);

                    $order_log_data['orderid'] = $orderid;
                    $order_log_data['price'] = $courseinfo->price;
                    $res2 = $order->insertOrderLogBySave($order_log_data);
                    // var_dump($res2);

                    $give_course_data['courseid'] = $courseid;
                    $give_course_data['member'] = intval($ust);
                    $res3 = $order->insertGiveCourseLogBySave($give_course_data);
                    // var_dump($res3);
                }
                if($res1 && $res2 && $res3){
                    $code = 200;
                }else{
                    $code = 400;
                }
            }
        }else{
            $code = 300;
        }

        $_data = array('code'=>$code);
        return response()->json($_data);
    }

    public function SetOrderBy(){
        $coursecontent = new CourseContentService;
        $courseid = intval($_POST['courseid']);
        $videoid = $_POST['videoid'];
        $orderby = $_POST['orderby'];

        foreach($orderby as $otm=>$ost){
            $id = $videoid[$otm];
            $data['video_name'] = $ost;
            $res = $coursecontent->modifyCoueseContent($data,$id);
        }

        if($res){
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }
    public function videoDel($id){
        date_default_timezone_set('PRC');
        $coursecontent = new CourseContentService;

        $data['isshow'] = 0;
        $data['update_date'] = date('Y-m-d H:i:s');

        $info = $coursecontent->getCourseContentInfoById($id);
        $res1 = OSS::deleteObject($info->video_link);
        $res2 = $coursecontent->modifyCoueseContent($data,$id);

        if($res1 || $res2){
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }

    public function videoUpdate(){
        date_default_timezone_set('PRC');
        $coursecontent = new CourseContentService;

        $id = intval($_POST['id']);

        $data['video_name'] = addslashes($_POST['video_name']);
        $data['video_length'] = intval($_POST['video_length']);
        $data['hits'] = intval($_POST['video_hits']);
        // $data['curriculum_id'] = intval($_POST['courseid']);
        $data['is_share'] = intval($_POST['isshare']);
        $data['is_free'] = intval($_POST['isfree']);
        $data['update_date'] = date('Y-m-d H:i:s');
        $data['video_link'] = addslashes($_POST['video_link']);

        $res = $coursecontent->modifyCoueseContent($data,$id);
        if($res){
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }
        return response()->json($_data);
    }

    public function videoEdit($id,$num){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $coursecontent = new CourseContentService;

            $data['username'] = $username;
            $data['contentinfo'] = $coursecontent->getCourseContentInfoById($id);
            $data['num'] = $num;
            return view('admin.course.video_edit',$data);
        }
    }

    public function viewVideoList($id){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $coursecontent = new CourseContentService;
            $course = new CourseService;
            $data['username'] = $username;
            $coursenum = $coursecontent->getCourseContentNum($id);
            $data['videolist'] = $coursecontent->getCourseContentList($coursenum,$id);
            $data['courseinfo'] = $course->getCourseInfoById($id);

            return view('admin.course.video_list',$data);
        }
    }

    public function upload(Request $request){
        $file_dir = $_POST['file'];
        $courseid = $_POST['courseid'];

        $file = $request->file('Filedata');

        if(!$file->isValid()){
            $data = array('code'=>400);
        }else{
            // 获取扩展名
            $ext = $file->getClientOriginalExtension();

            // 生成新文件名
            $newfile = md5(time().rand(1000,9999)).'.'.$ext;

            // 文件上传指定路径
            $path = public_path($file_dir);

            $target_file = $file_dir.'/video/'.$courseid.'/'.$newfile;

            $res = OSS::upload($target_file,$file->getRealPath());

            if($res){
                $data = array('code'=>200,'source_src'=>$target_file);
            }else{
                $data = array('code'=>400);
            }

            return response()->json($data);
        }

    }

    public function list(Request $request){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $data['username'] = $username;
            $data['page_size_list'] = array(20,25,50,100);
            $data['page_size'] = $request->input('page_size') ? intval($request->input('page_size')) : 15;
            $data['keywords'] = $request->input('keywords') ? addslashes($request->input('keywords')) : '';

            $course = new CourseService;
            $data['courselist'] = $course->getCourseList($data['page_size'],$data['keywords']);

            return view('admin.course.list',$data);
        }
    }

    public function create(){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $data['username'] = $username;

            // 获取课程分类
            $courseType = new CourseTypeService;
            $data['ctlist'] = $courseType->getTypeList();

            // 获取课程标签信息
            $tag = new TagService;
            $t_num = $tag->getTagCount();
            $data['taglist'] = $tag->getTagList($t_num,'');

            return view('admin.course.add',$data);
        }
    }

    public function store(){

        $course = new CourseService;

        $courseinfo = $course->getCourseInfoByName($_POST['name']);
        if(!empty($courseinfo)){
            $_data = array('code'=>300);
        }else{
            $res = $course->insertCourse($_POST);
            if($res){
                $_data = array('code'=>200,'course_id'=>$res);
            }else{
                $_data = array('code'=>400);
            }
        }

        return response()->json($_data);
    }

    public function edit($id){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $data['username'] = $username;
            $course = new CourseService;
            $coursecontent = new CourseContentService;
            $data['courseinfo'] = $course->getCourseInfoById($id);
            $coursenum = $coursecontent->getCourseContentNum($id);
            $data['coursevideolist'] = $coursecontent->getCourseContentList($coursenum,$id);

            // 获取课程分类
            $courseType = new CourseTypeService;
            //$ct_num = $courseType->getCourseTypeCount();
            $data['ctlist'] = $courseType->getTypeList();

            // 获取课程标签信息
            $tag = new TagService;
            $t_num = $tag->getTagCount();
            $data['taglist'] = $tag->getTagList($t_num,'');
            $data['courseid'] = $id;

            return view('admin.course.edit',$data);
        }
    }

    public function update(){
        date_default_timezone_set('PRC');
        $course = new CourseService;
        $coursecontent = new CourseContentService;

        $id = intval($_POST['id']);
        $data['name'] = addslashes($_POST['name']);
        $data['thumb'] = addslashes($_POST['title_pic']);
        $data['thumb2'] = addslashes($_POST['title_pic2']);
        $data['content'] = stripslashes(addslashes($_POST['content']));
        $data['tmid'] = intval($_POST['tmid']);
        $data['tid'] = intval($_POST['tid']);
        $data['desc'] = addslashes($_POST['desc']);
        $data['price'] = addslashes($_POST['price']);
        $data['is_buy_one'] = intval($_POST['isbuy']);
        $data['video_num'] = intval($_POST['videonum']);
        $data['curriculum_num'] = intval($_POST['coursenum']);
        $data['isfree'] = intval($_POST['is_free']);
        $data['update_date'] = date('Y-m-d H:i:s');
        $data['weight'] = intval($_POST['weight']);
        $res = $course->modifyCourse($id,$data);
        if($res){
            $content = addslashes($_POST['video_src']);
            if($content != ''){
                $contentinfo = $coursecontent->getVideoContent($id);
                $contentlist = explode(',',$content);
                foreach($contentlist as $k=>$v){
                    $_content['curriculum_id'] = $id;
                    $_content['video_link'] = $v;
                    $_content['video_name'] = '';
                    $_content['add_date'] = date('Y-m-d H:i:s');
                    $_content['isshow'] = 1;

                    $coursecontent->insertContent($_content);
                }
            }
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }

    public function del($id){
        date_default_timezone_set('PRC');
        $course = new CourseService;

        $data['isshow'] = 0;
        $data['update_date'] = date('Y-m-d H:i:s');

        $res = $course->modifyCourse($id,$data);
        if($res){
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }
}